﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Business;
using Ecreate.Model;

namespace Ecreate.Web.pages
{
    public partial class order : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string stype = Request.QueryString["type"].ToString();
                utility.SessionObject so = utility.SessionManage.getSession(this);
                if(!Valid(stype, so)) return;
                if (stype == "ht")
                {
                    ucHeader1.Visible = false;
                    txtPayAccount.Text = so.Admin.Account;
                }
                if (stype == "qt")
                {
                    txtPayAccount.Text = so.Customer.Cus_account;
                }
                //string id = Request.QueryString["id"];
                //if (id != null && id != "")
                //{
                //    OrderService service = new OrderService();
                //    IList list = service.SelectByID(Convert.ToInt32(id));
                //    Order o = list[0] as Order;
                //    txtSellAddress.Text = o.SellAddress;
                //    txtSellAddress.Text = o.SellPhoneNumber;
                //    txtProduct.Text = o.Product.ToString();
                //    txtPrice.Text = o.Price.ToString();
                //    txtCustName.Text = o.CustName;
                //    txtCustAddress.Text = o.CustAddress;
                //    txtCustPhoneNumber.Text = o.CustPhoneNumber;
                //    txtPayAccount.Text = o.PayAccount;
                //    btnInsert.Visible = false;
                //}
                //AreaService service = new AreaService();
                //BindDDLArea(ddlProvince, service.getProvinceArea());
                //if (ddlProvince.SelectedIndex != -1)
                //{
                //    ddlProvince_SelectedIndexChanged(null, null);
                //}
                BindDDLProductClass(ddlClass1, "-1");
                if (ddlClass1.SelectedIndex != -1)
                {
                    ddlClass1_SelectedIndexChanged(null, null);
                }
                txtPayDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        private bool Valid(string stype, utility.SessionObject so)
        {
            string msg = "";
            bool flag = true;
            if (so == null)
            {
                msg = "请先登陆。";
                flag = false;
            }
            else
            {
                if ((stype == "qt" && so.Usertype != Ecreate.Web.utility.LoginUserType.customer)
                    || (stype == "ht" && so.Usertype != Ecreate.Web.utility.LoginUserType.admin)
                    || (stype == "ht" && so.Usertype == Ecreate.Web.utility.LoginUserType.admin && so.Admin.Group.IsAdmin))
                {
                    msg = "该用户不能录入订单。";
                    flag = false;
                }
            }
            if (!flag)
            {
                if (stype == "qt")
                {
                    AlertAndRedirect(msg, "Default.aspx");
                }
                else if (stype == "ht")
                {
                    AlertAndRedirect(msg, "popedom/login/login.aspx");
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
            return flag;
        }

        //protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    string areaCode = ddlProvince.SelectedValue;
        //    AreaService service = new AreaService();
        //    BindDDLArea(ddlCity, service.getCityArea(areaCode));
        //    if (ddlCity.SelectedIndex != -1)
        //    {
        //        ddlCity_SelectedIndexChanged(null, null);
        //    }
        //}

        //protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    string areaCode = ddlCity.SelectedValue;
        //    AreaService service = new AreaService();
        //    BindDDLArea(ddlCounty, service.getCountryArea(areaCode));
        //}

        //private void BindDDLArea(DropDownList ddl, IList list)
        //{
        //    BindDDL(ddl, list, "AreaName", "AreaCode");
        //}

        protected void ddlClass1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string parentclassid = ddlClass1.SelectedValue;
            BindDDLProductClass(ddlClass2, parentclassid);
            if (ddlClass2.SelectedIndex != -1)
            {
                ddlClass2_SelectedIndexChanged(null, null);
            }
        }

        protected void ddlClass2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ProductService service = new ProductService();
            Product p = new Product();
            p.Classid = ddlClass2.SelectedValue;
            BindDDL(ddlProduct, service.getProductByClassID(p), "pname", "pid");
            if (ddlProduct.SelectedIndex != -1)
            {
                GetPrice();
            }
        }

        private void BindDDLProductClass(DropDownList ddl, string parentclassid)
        {
            ProductClassService service = new ProductClassService();
            BindDDL(ddl, service.getProductClassByParentID(parentclassid), "classname", "classid");
        }

        private void BindDDL(DropDownList ddl,IList list, string TextField, string ValueField)
        {
            ddl.DataSource = list;
            ddl.DataTextField = TextField;
            ddl.DataValueField = ValueField;
            ddl.DataBind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                utility.SessionObject so = utility.SessionManage.getSession(this);
                string stype = Request.QueryString["type"].ToString();
                if(!Valid(stype, so)) return;
                string customerid = "";
                if (stype == "qt")
                {
                    customerid = so.Customer.Cus_id;
                }
                else
                {
                    customerid = so.Admin.Id;
                }
                Order o = new Order();
                try
                {
                    //o.SellAddress = txtSellAddress.Text;
                    //o.SellPhoneNumber = txtSellPhoneNumber.Text;
                    if (ddlProduct.SelectedIndex != -1)
                    {
                        o.Product = Convert.ToInt32(ddlProduct.SelectedValue);
                    }
                    o.Price = decimal.Parse(txtPrice.Text);
                    o.CustName = txtCustName.Text;
                    o.CustAddress = txtCustAddress.Text;
                    o.CustPhoneNumber = txtCustPhoneNumber.Text;
                    //o.PayAccount = txtPayAccount.Text;
                    o.PayDate = Convert.ToDateTime(txtPayDate.Text);
                    o.CustomerID = customerid;
                    //if (ddlProvince.SelectedIndex != -1)
                    //{
                    //    o.Province = ddlProvince.SelectedValue;
                    //}
                    //if (ddlCity.SelectedIndex != -1)
                    //{
                    //    o.City = ddlCity.SelectedValue;
                    //}
                    //if (ddlCounty.SelectedIndex != -1)
                    //{
                    //    o.County = ddlCounty.SelectedValue;
                    //}
                    if (ddlClass1.SelectedIndex != -1)
                    {
                        o.ProductClass1 = ddlClass1.SelectedValue;
                    }
                    if (ddlClass2.SelectedIndex != -1)
                    {
                        o.ProductClass2 = ddlClass2.SelectedValue;
                    }
                    o.QQ = txtQQ.Text;
                }
                catch
                {
                    Alert("录入信息有误，请校正后重新提交。");
                    return;
                }
                OrderService service = new OrderService();
                service.Insert(o);
                Alert("提交成功。");
                //string msg = "提交成功。";
                //if (stype == "qt")
                //{
                //    AlertAndRedirect(msg, "Default.aspx");
                //}
                //else if (stype == "ht")
                //{
                //    AlertAndRedirect(msg, "popedom/login/login.aspx");
                //}
                //else
                //{
                //    Response.Redirect("pages/Error.aspx");
                //}
                //Session["order"] = o;
                //Response.Redirect("order.aspx");
            }
            catch
            {
                Alert("提交失败。");
            }
        }

        private void Alert(string message)
        {
            Response.Write("<script type=\"text/javascript\" language=\"javascript\">        alert('" + message + "');　; </script>");
        }

        private void AlertAndRedirect(string message,string page)
        {
            Response.Write("<script type=\"text/javascript\" language=\"javascript\">alert('" + message + "');window.location.href=\"/" + page + "\";</script>");
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            txtPayDate.Text = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
        }

        protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetPrice();
        }

        private void GetPrice()
        {
            ProductService service = new ProductService();
            Product p = new Product();
            p.Pid = Convert.ToInt32(ddlProduct.SelectedValue);
            Product pp = service.getProductByID(p);
            txtPrice.Text = pp.Price.ToString();
        }
    }
}
